/*  
 * @(#) SparkTest1.java Create on 2015年2月2日 下午12:08:49   
 *   
 * Copyright 2015 jstx.
 */

package test.spark;

import static org.junit.Assert.*;

import java.util.Arrays;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.VoidFunction;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * @SparkTest1.java
 * @created at 2015年2月2日 下午12:08:49 by zhanghongliang@hiveview.com
 *
 * @desc
 *
 * @author zhanghongliang@hiveview.com
 * @version $Revision$
 * @update: $Date$
 */
public final class SparkTest1 {

	private static JavaSparkContext ctx;

	public static void init2() {
		SparkConf sparkConf = new SparkConf().setAppName("JavaWordCount");
		sparkConf.setMaster("local");
		ctx = new JavaSparkContext(sparkConf);
	}

	public static void main(String[] args) throws Exception {
		init2();
		
		String path = "E:/.tmp/down/readme";
		System.out.println(path);
		JavaRDD<String> javaRdd = ctx.textFile(path);
		
		javaRdd = javaRdd.flatMap(new FlatMapFunction<String, String>() {
			@Override
			public Iterable<String> call(String t) throws Exception {
				System.out.println("flat map ================================");
				System.out.println("\t"+t);
				return Arrays.asList(new String[]{t});
			}
		});
		
		System.out.println("map......................................");
		javaRdd.map(new Function<String, String>() {
			@Override
			public String call(String v1) throws Exception {
				System.out.println("---------------------------------------");
				System.out.println("\t"+v1);
				return v1;
			}
		});
		
		System.out.println("foreach......................................");
		javaRdd.foreach(new VoidFunction<String>() {
			@Override
			public void call(String t) throws Exception {
				System.out.println("foreach---------------------------------------------");
				System.out.println("\t"+t);
				System.out.println("foreach end---------------------------------------------");
			}
		});
	}
}
